<?php

class SmtpController extends MyController
{	
	function init() {parent::init(); }
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}
	
	function actionView($page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->website["beperpage"];

		$rows = NULL;

		$rows = smtp::model()->findAll(array(
			"order" => "`order` ASC",
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		// tong so dong
		$pages->itemCount =  smtp::model()->count();
			
		$this->data['rows']= $rows;		
		
		$this->data['pages']= $pages;	
		$this->data['page']= $page;	
		$this->render('view', $this->data);
	}
	
	function actionAdd($page = 1)
	{	
		$this->action = "Thêm mới";
		$row = new smtp;
		$max_order = Yii::app()->db->createCommand(
			"SELECT max(`order`)
			FROM smtp"
		)->queryScalar();
						
		$row->id = 0;
		$row->order = $max_order + 1;
		$row->smtp_port = 25;
		
		$this->data["row"] = $row;
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($smtp_id = 0, $page = 1)
	{
		$row = NULL;
		if ($smtp_id == 0)
		{
			$this->redirect(array($this->controller_id.'/view', "page"=>$page));
		}else{	
			$row = smtp::model()->findByPk($smtp_id);
		}
		
		$this->data["row"] = $row;
		$this->data["page"] = $page;
		
		$this->action = "Cập nhật";
		$this->render("form_edit", $this->data);
	}
	
	function actionSave($smtp_id = 0, $page = 0)
	{
		if ($smtp_id == 0) // new smtp
		{	
			$row = new smtp;
			$row->setAttributes($_POST, false);
			if (strlen($row->name))
				$row->save();
		}
		else
		{
			$row = smtp::model()->findByPk($smtp_id);
			$row->setAttributes($_POST, false);
			if(strlen($row->name))
				$row->save();
		}
		$this->redirect(array($this->controller_id."/view", "page"=>$page));
	}
	
	function actionDelete($smtp_id, $page= 1)
	{
		smtp::model()->deleteByPk($smtp_id);
		
		$rows = smtp::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if($page > 1)
				$page--;
		}
		
		$this->redirect(array($this->controller_id.'/view',"page"=>$page));
	}
	
	function actionEnable($smtp_id = 0, $page = 1)
	{
		$row = smtp::model()->findByPk($smtp_id);
		$row->enable = 1 - $row->enable;
		$row->save();	
		$this->redirect(array($this->controller_id."/view", "page"=>$page ));
	}

	function actionDefault($smtp_id = 0, $page = 1)
	{
		smtp::model()->updateAll(array("default"=>0));
		$row = smtp::model()->findByPk($smtp_id);
		$row->default = 1;
		$row->save();	
		$this->redirect(array($this->controller_id."/view", "page"=>$page ));
	}
	
	function actionDelete_multi($page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		
		if (!is_null($id_array))
		{			
			foreach($id_array as $id=>$value)
			{
				smtp::model()->deleteByPk($id);
			}
		}
		
		$rows = smtp::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if($page > 1)
				$page--;
		}
		
		$this->redirect(array($this->controller_id.'/view',  "page"=>$page));
	}
	
	function actionSet_order($page = 1)
	{
		//Tao thu tu mac dinh
		$rows = smtp::model()->findAll();
		foreach($rows as $row)
		{
			$row->order = $row->id;
			$row->save();
		}
		
		$this->redirect(array($this->controller_id.'/view', "page" => $page));		
	}
	
	function actionSave_order($page = 1)
	{
		$orders = Yii::app()->getRequest()->getPost("order");
		
		foreach($orders as $id=>$value)
		{
			$row = smtp::model()->findByPk($id);
			$row->order = $value;
			$row->save();
		}
		
		//print_r($orders);
		$this->redirect(array($this->controller_id.'/view', "page" => $page));		
	}
	
}